const app = getApp();

Page({
  data: {
    habits: [],
    filterType: 'all',
    today: new Date().toDateString()
  },
  
  onLoad() {
    this.loadHabits();
  },
  
  onShow() {
    this.loadHabits();
  },
  
  loadHabits() {
    const habits = app.globalData.habits;
    let filteredHabits = habits;
    
    // 根据过滤类型筛选习惯
    if (this.data.filterType === 'completed') {
      filteredHabits = habits.filter(habit => habit.history[this.data.today] === true);
    } else if (this.data.filterType === 'incomplete') {
      filteredHabits = habits.filter(habit => habit.history[this.data.today] !== true);
    }
    
    this.setData({
      habits: filteredHabits
    });
  },
  
  setFilter(e) {
    const filterType = e.currentTarget.dataset.type;
    this.setData({ filterType }, () => {
      this.loadHabits();
    });
  },
  
  toggleHabit(e) {
    const habitId = e.currentTarget.dataset.id;
    const today = this.data.today;
    const habits = app.globalData.habits;
    const habitIndex = habits.findIndex(h => h.id === habitId);
    
    if (habitIndex === -1) return;
    
    const habit = habits[habitIndex];
    const dayOfWeek = new Date().getDay();
    
    // 检查今天是否在频率内
    if (!habit.frequency.includes(dayOfWeek)) {
      wx.showToast({
        title: '今天不是该习惯的执行日',
        icon: 'none'
      });
      return;
    }
    
    // 切换完成状态
    if (habit.history[today] === true) {
      habit.history[today] = false;
      habit.streak = Math.max(0, habit.streak - 1);
    } else {
      habit.history[today] = true;
      
      // 检查是否连续
      const yesterday = new Date();
      yesterday.setDate(yesterday.getDate() - 1);
      const yesterdayStr = yesterday.toDateString();
      
      if (habit.history[yesterdayStr] === true) {
        habit.streak++;
      } else {
        habit.streak = 1;
      }
      
      // 更新最长连续打卡
      if (habit.streak > habit.longestStreak) {
        habit.longestStreak = habit.streak;
      }
    }
    
    // 保存数据
    app.saveHabits();
    
    // 更新界面
    this.loadHabits();
    
    // 更新统计页面
    const pages = getCurrentPages();
    const statisticsPage = pages.find(page => page.route === 'pages/statistics/statistics');
    if (statisticsPage) {
      statisticsPage.updateStatistics();
    }
    
    // 更新日历页面
    const calendarPage = pages.find(page => page.route === 'pages/calendar/calendar');
    if (calendarPage) {
      calendarPage.renderCalendar();
    }
  },
  
  deleteHabit(e) {
    const habitId = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这个习惯吗？',
      success: (res) => {
        if (res.confirm) {
          // 删除习惯
          app.globalData.habits = app.globalData.habits.filter(h => h.id !== habitId);
          app.saveHabits();
          
          // 更新界面
          this.loadHabits();
          
          // 更新统计页面
          const pages = getCurrentPages();
          const statisticsPage = pages.find(page => page.route === 'pages/statistics/statistics');
          if (statisticsPage) {
            statisticsPage.updateStatistics();
          }
          
          // 更新日历页面
          const calendarPage = pages.find(page => page.route === 'pages/calendar/calendar');
          if (calendarPage) {
            calendarPage.renderCalendar();
          }
          
          wx.showToast({
            title: '习惯已删除',
            icon: 'success'
          });
        }
      }
    });
  },
  
  navigateToAddHabit() {
    wx.navigateTo({
      url: '/pages/add-habit/add-habit'
    });
  }
});    